from flask import Flask
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()

def create_app():    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:abc123456@userdb:3306/user'    
    db.init_app(app)
    return app

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(200))
    userinfo_id = db.Column(db.Integer, db.ForeignKey('userinfo.id'))
    userinfo = db.relationship('Userinfo',
        backref=db.backref('userinfo'))

    def __init__(self, username, password, userinfo):
        self.username = username
        self.password = password
        self.userinfo = userinfo

    def __repr__(self):
        return '<User %r>' % self.username

class Userinfo(db.Model):
    __tablename__ = 'userinfo'
    __table_args__ = {
        "mysql_charset": "utf8"
    }
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    studentid = db.Column(db.String(10))
    gender = db.Column(db.String(1))

    def __init__(self, name, studentid, gender):
        self.name = name
        self.studentid = studentid
        self.gender = gender

    def __repr__(self):
        return '<User %r>' % self.name